
var metaData = {
requestTypes: [
                { id: -1, label: '签卡' },
                { id: 0, label: '调休' },
                { id: 1, label: '年假' },
                { id: 2, label: '独生子女陪护假' },
                { id: 3, label: '婚假' },
                { id: 4, label: '丧假' },
                { id: 5, label: '事假' },
                { id: 6, label: '病假' },
                { id: 7, label: '加班' },
                { id: 8, label: '出差' },
                { id: 9, label: '外勤' },
                { id: 10, label: '哺乳假' },
                { id: 11, label: '陪产假' },
                { id: 12, label: '工伤假' },
                { id: 13, label: '产假' },
                // { id: 14, label: '特殊情况' },
                { id: 15, label: '医疗期' },
                { id: 16, label: '运动日' },
                { id: 17, label: '其它法定带薪假' },
                { id: 18, label: '产检假' }
            ],
            requestLeaveTypes: [
                { id: 0, label: '调休', ruleCode: 'CompensatoryLeave' },
                { id: 1, label: '年假', ruleCode: 'AnnualLeave' },
                { id: 2, label: '独生子女陪护假', ruleCode: 'OnlyChildPaternityLeave' },
                { id: 3, label: '婚假', ruleCode: 'MarriageLeave' },
                { id: 4, label: '丧假', ruleCode: 'BereavementLeave' },
                { id: 5, label: '事假', ruleCode: 'PersonalLeave' },
                { id: 6, label: '病假', ruleCode: 'SickLeave' },
                { id: 10, label: '哺乳假', ruleCode: 'LactationLeave' },
                { id: 11, label: '陪产假', ruleCode: 'PaternityLeave' },
                { id: 12, label: '工伤假', ruleCode: 'InjuryLeave' },
                { id: 13, label: '产假', ruleCode: 'MaternityLeave' },
                // { id: 14, label: '特殊情况',ruleCode:''  },
                { id: 15, label: '医疗期', ruleCode: 'MedicalPeriod' },
                { id: 16, label: '运动日', ruleCode: 'SportsDay' },
                { id: 17, label: '其它法定带薪假', ruleCode: 'OtherLeave' },
                { id: 18, label: '产检假', ruleCode: 'Examination' }
            ],
            //  '审批状态:\n0.撤回\n1.审批中\n2.审批同意\n3.审批拒绝 4 草稿 5 已废弃',
            approvalState: [
                { id: 0, label: '撤回' },
                { id: 1, label: '审批中' },
                { id: 2, label: '审批通过' },
                { id: 3, label: '审批拒绝' },
                // { id: 4, label: '草稿' },
                // { id: 5, label: '已废弃' },
                { id: 6, label: '已作废' }
            ]
        }

function getSqlTypeTpl() {
    var SQL_TYPE_TPL = "INSERT INTO `os_kernel_appsysctr_dictionary_type` (`type_name`, `type_code`, `parent_id`, `status`, `remark`, `creator`, `create_time`, `modifier`, `modify_time`, `del_flag`, `sysid`) VALUES ('{0}', '{1}', '0', '1', '{2}', 'fhl', '2021-06-03 03:24:58', NULL, NULL, '0', 'fhl');";
    return SQL_TYPE_TPL;
}

function getSqlItemTpl() {
    var SQL_ITEM_TPL = "INSERT INTO `os_kernel_appsysctr_dictionary_item` (`item_name`, `item_code`, `type_id`, `order_no`, `status`, `remark`, `creator`, `create_time`, `modifier`, `modify_time`, `del_flag`) VALUES ('{0}', '{1}', (select id from os_kernel_appsysctr_dictionary_type where type_code = '{2}'), '{3}', '1', '{4}', 'fhl', NULL, NULL, NULL, '0');";
    return SQL_ITEM_TPL;
}

function getUpdateItemSqlTpl() {
    var SQL_UPDATE_ITEM_TPL = "update `os_kernel_appsysctr_dictionary_item` set remark = '{0}' where item_code = '{1}' and item_name = '{2}' and type_id = (select id from os_kernel_appsysctr_dictionary_type where type_code = 'requestLeaveTypes');";
    return SQL_UPDATE_ITEM_TPL;
}

function deleteFile(fs, fileName) {
        fs.exists(fileName, (exists)=>{
         if (exists) {
          console.log("文件已存在");
         console.log("准备删除文件！");
        fs.unlink(fileName, function(err) {
        if (err) {
            return console.error(err);
        }
        console.log("文件删除成功！");
        });
     } else {
          console.log("文件不存在");
     }
    });
}

function write2File(fs,fileName, content) {
    console.log("准备写入文件");
    fs.appendFileSync(fileName, content,  function(err) {
    if (err) {
        return console.error(err);
    }
    console.log("数据写入成功！");
    });
}

var typeDict = {
    requestTypes: '请假类型',
    requestLeaveTypes: '假期类型', 
    approvalState: '审批状态'
}

var fs = require("fs");
console.log(fs);
deleteFile(fs, 'dict_script.sql');
deleteFile(fs, 'update_script.sql');
var prefix = '';
for (const key in metaData) {
   console.log(key) 
   var typeName = typeDict[key];
   var sqlTypeTpl = getSqlTypeTpl();
   sqlTypeTpl = sqlTypeTpl.replace("{0}", typeName);
   sqlTypeTpl = sqlTypeTpl.replace("{1}", prefix+key);
   sqlTypeTpl = sqlTypeTpl.replace("{2}", typeName);
   console.log(sqlTypeTpl)
   write2File(fs, 'dict_script.sql',"\r\n"+sqlTypeTpl+"\r\n");
   var sqlItemTpl = getSqlItemTpl();
   var itemArr = metaData[key];
   var sortNo = 0;
   for(var idx in itemArr) {
       console.log(itemArr[idx]);
       var obj = itemArr[idx];
       var sqlItemTpl = getSqlItemTpl();
       sqlItemTpl = sqlItemTpl.replace('{0}', obj.label);
       sqlItemTpl = sqlItemTpl.replace('{1}', obj.id);
       sqlItemTpl = sqlItemTpl.replace('{2}', key);
       sqlItemTpl = sqlItemTpl.replace('{3}', ++sortNo);
       sqlItemTpl = sqlItemTpl.replace('{4}', obj.ruleCode!=undefined?obj.ruleCode:'fhl20210603');
       write2File(fs,'dict_script.sql', sqlItemTpl+'\r\n');

       if(key == 'requestLeaveTypes') {
            var updateItemSqlTpl = getUpdateItemSqlTpl();
            updateItemSqlTpl = updateItemSqlTpl.replace('{0}', obj.ruleCode);
            updateItemSqlTpl = updateItemSqlTpl.replace('{1}', obj.id);
            updateItemSqlTpl = updateItemSqlTpl.replace('{2}', obj.label);
            write2File(fs, 'update_script.sql',updateItemSqlTpl + '\r\n');
       }
   }
}


